package com.flame.application.service;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import reactor.core.publisher.Flux;

/**
 * @author Jflame
 * @version 1.0.0
 * @since 2025/8/15
 */
@Service
public class ChatService {
    private final ChatClient chatClient;

    public ChatService(ChatClient.Builder builder) {
        this.chatClient = builder.build();
    }

    public Flux<String> chat(String message, String conversationId) {
        String finalConversationId = StringUtils.hasText(conversationId) ?
                conversationId : ChatMemory.DEFAULT_CONVERSATION_ID;

        return chatClient
                .prompt()
                .user(message)
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, finalConversationId))
                .stream()
                .content();
    }
}
